﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using WssUtils;

namespace WssEvents
{

    static class MessageConfig
    {
        const string GAME_START = "_$_attack";
    }

    //todo 改进，让 MessageCenter 功能更强大，支持 0 ~ 4  个 参数 的 使用 


    /// <summary>
    /// 全局消息通知
    /// </summary>
    [System.Obsolete(" Use GlobalEvent instad of MessageCenter!")]
    public class MessageCenter : Singleton<MessageCenter>
    {

        /// <summary>
        /// 传递事件的数据(一个包装类，方便处理信息)
        /// </summary>
        public struct EventData
        {
            object param0;
            object param1;
            object param2;
            object param3;
            object param4;

            private void SetData(object p0,object p1,object p2, object p3 , object p4)
            {
                param0 = p0;
                param1 = p1;
                param2 = p2;
                param3 = p3;
                param4 = p4;
            }

            private object GetObject(int id)
            {
                switch (id)
                {
                    case 0: return param0;
                    case 1: return param1;
                    case 2: return param2;
                    case 3: return param3;
                    case 4: return param4;
                    default:
                        return param0;
                }
            }
            public string CorventToString(int id)
            {
                return System.Convert.ToString(GetObject(id));
            }

            public int CorventToInt(int id)
            {
                return System.Convert.ToInt32(GetObject(id));
            }

            public float CorventToFloat(int id)
            {
                return System.Convert.ToSingle(GetObject(id));
            }

            public bool CorventToBool(int id)
            {
                return System.Convert.ToBoolean(GetObject(id));
            }


        }

        public class CommonEvent : UnityEvent { };
        public class CommonEventWithData : UnityEvent<EventData> { };

        //事件列表
        private Dictionary<string, CommonEvent> mDicEvents0 = new Dictionary<string, CommonEvent>();
        private Dictionary<string, CommonEventWithData> mDicEvents1 = new Dictionary<string, CommonEventWithData>();


        /// <summary>
        /// 初始化，释放所有回调
        /// </summary>
        public void Init()
        {
            ClearAll();
        }


        /// <summary>
        ///  添加一个回调事件
        /// </summary>
        private CommonEvent GetAddAction0(string key)
        {
            if (mDicEvents0.ContainsKey(key) == false) mDicEvents0.Add(key, new CommonEvent());
            return mDicEvents0[key];

        }
        private CommonEventWithData GetAddAction1(string key)
        {
            if (mDicEvents1.ContainsKey(key) == false) mDicEvents1.Add(key, new CommonEventWithData());
            return mDicEvents1[key];

        }


        /// <summary>
        /// 事件触发
        /// </summary>
        public void Emit(string key)
        {
            if (mDicEvents0.ContainsKey(key))
            {
                var action = mDicEvents0[key];
                action.Invoke();
            }
            else
            {
                Debug.LogWarning("[emit]没有这个定义的事件:" + key);
            }

        }
        public void Emit(string key, EventData param)
        {
            if (mDicEvents1.ContainsKey(key))
            {
                var action = mDicEvents1[key];
                action.Invoke(param);
            }
            else
            {
                Debug.LogWarning("[emit]没有这个定义的事件:" + key);
            }

        }



        /// <summary>
        /// 事件绑定
        /// </summary>
        public void On(string key, UnityAction attachEvent)
        {
            var action = GetAddAction0(key);
            action.AddListener(attachEvent);
        }
        public void On(string key, UnityAction<EventData> attachEvent)
        {
            var action = GetAddAction1(key);
            action.AddListener(attachEvent);
        }

        /// <summary>
        /// 去除事件绑定
        /// </summary>
        public void Off(string key, UnityAction attachEvent)
        {
            if (mDicEvents0.ContainsKey(key))
            {
                mDicEvents0[key].RemoveListener(attachEvent);
            }
            else
            {
                Debug.LogWarning("[off]没有这个定义的事件:" + key);
            }
        }
        public void Off(string key, UnityAction<EventData> attachEvent)
        {
            if (mDicEvents1.ContainsKey(key))
            {
                mDicEvents1[key].RemoveListener(attachEvent);
            }
            else
            {
                Debug.LogWarning("[off]没有这个定义的事件:" + key);
            }
        }



        /// <summary>
        /// 移除这个key 的所有事件
        /// </summary>
        public void Remove(string key)
        {
            if (mDicEvents0.ContainsKey(key))
            {
                mDicEvents0.Remove(key);
            }

            if (mDicEvents1.ContainsKey(key))
            {
                mDicEvents1.Remove(key);
            }
        }

        public void ClearAll()
        {
            mDicEvents0.Clear();
            mDicEvents1.Clear();
        }


    }


}

